package com.song.EMS.controller;


import com.song.EMS.bean.Administrator;


import com.song.EMS.bean.Message;
import com.song.EMS.mvc.ResponseBody;
import com.song.EMS.service.impl.AdministratorServiceImpl;
import com.song.EMS.utils.JsonUtil;
import com.song.EMS.utils.TransformUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AdministratorController {
    private AdministratorServiceImpl administratorService = new AdministratorServiceImpl();
    private Message data = null;

    /**
     * 管理员的登录
     * @param request
     * @param response
     * @return 给前端的ajxa信息
     */
    @ResponseBody("/admin/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        //从前端获取信息
        String administratorName = request.getParameter("administratorname");

        String password = request.getParameter("password");

        Administrator administrator = administratorService.login(administratorName, password);


        if (administrator != null) {
            request.getSession().setAttribute("administratorName", administratorName);
            request.getSession().setAttribute("administratorPhone",administrator.getAdministratorPhone());
            //登录成功
            data = new Message(1,"登录成功");
            //更新IP与登录时间
            this.administratorService.updateLoginTime(administratorName,request.getRemoteAddr(),TransformUtil.getTimestamp());
        }else{
            //登录失败
            data = new Message(-1,"登陆失败");
        }
        return JsonUtil.toJSON(data);

    }

    /**
     * 管理员退出登录
     * @param request
     * @param response
     */
    @ResponseBody("/admin/loginOut.do")
    public String loginOut(HttpServletRequest request, HttpServletResponse response){
        System.out.println("退出");
        //清除session中的存值
        request.getSession().invalidate();

        Message data = new Message(1,"退出成功");
        return JsonUtil.toJSON(data);

    }

    /**
     * 管理员的注册
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/admin/register.do")
    public String register(HttpServletRequest request, HttpServletResponse response){
        //获取前端页面ajxa数据
        String administratorName = request.getParameter("administratorname");
        String password = request.getParameter("password");
        String administratorPhone = request.getParameter("phone");
        System.out.println("************");
        Administrator administrator = new Administrator(administratorName, password, administratorPhone, TransformUtil.getTimestamp());
        Integer register = administratorService.register(administrator);

        if (register == 1){
            data = new Message(1,"注册成功");
        }else if (register == 0){
            data = new Message(0,"管理员名已被注册");
        }else if (register == -1){
            data = new Message(-1,"手机号码已被注册");
        }else{
            data = new Message(-2,"注册失败");
        }
        return JsonUtil.toJSON(data);
    }

}
